/**
 * Copyright (c) 2023 murenchao
 * taomu is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.toolkit.jersey

import com.google.inject.Injector
import com.google.inject.Key
import com.google.inject.TypeLiteral
import com.google.inject.name.Names
import cool.taomu.toolkit.TaomuGuice
import io.swagger.jaxrs.listing.AcceptHeaderApiListingResource
import io.swagger.jaxrs.listing.ApiListingResource
import io.swagger.jaxrs.listing.SwaggerSerializers
import java.util.ArrayList
import java.util.List
import javax.inject.Inject
import javax.servlet.ServletContext
import org.apache.commons.lang3.StringUtils
import org.glassfish.hk2.api.ServiceLocator
import org.glassfish.jersey.media.multipart.MultiPartFeature
import org.glassfish.jersey.server.ResourceConfig
import org.glassfish.jersey.server.mvc.freemarker.FreemarkerMvcFeature
import org.jvnet.hk2.guice.bridge.api.GuiceBridge
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge
import org.slf4j.LoggerFactory

class GuiceResourceConfig extends ResourceConfig {
    val static LOG = LoggerFactory.getLogger(GuiceResourceConfig);

    new() {
        super();
    }

    @Inject
    new(ServiceLocator serviceLocator, ServletContext servletContext) {
        var injector = servletContext.getAttribute(Injector.name) as Injector;
        var pks = new ArrayList<String>();
        pks.add("io.swagger.jaxrs.listing");
        try {
            var pk = injector.getInstance(Key.get(new TypeLiteral<List<String>>() {
            }, Names.named(TaomuGuice.JESERY_PACKAGES)));
            // pks.addAll(pk.get(new TypeToken<List<String>>() { }));
            pks.addAll(pk)
        } catch (Exception ex) {
            LOG.debug("packages Exception:{}", ex);
        }
        try {
            var String slpk = injector.getInstance(
                Key.get(String, Names.named(TaomuGuice.JESERY_SERVICELOADER_PACKAGES)));
            pks.add(slpk);
        } catch (Exception ex) {
            LOG.debug("packages Exception:{}", ex);
        }
        var pk = pks.join(",")
        LOG.info("jesery 扫描的的 packages:\n{}", pk);
        packages(pk.split(","));
        register(MultiPartFeature);
       // try {
        	var templateKey = Key.get(String, Names.named("jesery.templatePath"))
        	if(injector.getExistingBinding(templateKey) !== null){
            	var String tmpPath = injector.getInstance(templateKey);
	            LOG.info("页面模板路径：{}", tmpPath);
	            property(FreemarkerMvcFeature.TEMPLATE_BASE_PATH, tmpPath);
            }else{
	            LOG.info("没有配置模版路径使用默认路局:freemarker");
	            property(FreemarkerMvcFeature.TEMPLATE_BASE_PATH, "freemarker");
            }
        //} catch (Exception ex) {
         //   LOG.debug("packages Exception:{}", ex);
          //  LOG.info("加载默认页面模板路径：freemarker");
           // property(FreemarkerMvcFeature.TEMPLATE_BASE_PATH, "freemarker");
        //}
        register(FreemarkerMvcFeature);
        GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
        var GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge);
        guiceBridge.bridgeGuiceInjector(injector);

        register(ApiListingResource);
        register(AcceptHeaderApiListingResource);
        register(SwaggerSerializers);

    }

}
